package utils.core
{
	import flash.net.SharedObject;

	/**
	 * ShareObject 工具类
	 * @author abram
	 **/ 
	public class ShareObjectUtils
	{
		public function ShareObjectUtils()
		{
		}
		
		/**
		 * 根据名字获取shareObject的值
		 * @author abram
		 * @param shareObjectName
		 * @param key
		 **/  
		public static function getData(shareObjectName:String,key:String):Object
		{
			try
			{
				var shareObject:SharedObject=SharedObject.getLocal(shareObjectName);
				if(shareObject&&shareObject.data[key])
					return shareObject.data[key];
				else
					return null;
			}
			catch(ex:Error){}
			return null;
		}
		
		/**
		 * 清除所有缓存
		 * @author abram
		 * @param url 缓存地址
		 **/ 
		public static function clearAll(url:String):void
		{
			try
			{
				SharedObject.deleteAll(url);
			}
			catch(ex:Error){}
		}
		
		/**
		 * 清除name指定的缓存
		 * @author abram
		 * @param shareObjectName
		 **/ 
		public static function clear(shareObjectName:String):void
		{
			try
			{
				var shareObject:SharedObject=SharedObject.getLocal(shareObjectName);
				shareObject.clear();
			}
			catch(ex:Error){};
		}
		
		/**
		 * 删除缓存中指定的key
		 * @author abram
		 * @param shareObjectName
		 * @param key
		 **/ 
		public static function deleteKey(shareObjectName:String,key:String):void
		{
			var shareObject:SharedObject=SharedObject.getLocal(shareObjectName);
			if(shareObject&&shareObject.data[key])
			{
				shareObject.data[key]=null;
				delete shareObject.data[key];
			}
		}
		
		/**
		 * 保存缓存
		 * @author abram
		 * @param shareObjectName
		 * @param key
		 * @param value
		 * @return boolean
		 **/ 
		public static function save(shareObjectName:String,key:String,value:Object):Boolean
		{
			try
			{
				var shareObject:SharedObject=SharedObject.getLocal(shareObjectName);
				shareObject.data[key]=value;
				shareObject.flush();
				return true;
			}catch(ex:Error){}
			return false;
		}
		
	}
}